package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WaterMeterinstallinfo;
import com.ruoyi.system.domain.WaterWorkOrder;
import com.ruoyi.system.mapper.WaterMeterinstallinfoMapper;
import com.ruoyi.system.service.IWaterMeterService;
import com.ruoyi.system.service.IWaterMeterinstallinfoService;
import com.ruoyi.system.service.IWaterUserService;
import com.ruoyi.system.service.IWaterWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 表具安装Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-16
 */
@Service
public class WaterMeterinstallinfoServiceImpl implements IWaterMeterinstallinfoService
{
    @Autowired
    private WaterMeterinstallinfoMapper waterMeterinstallinfoMapper;

    @Autowired
    private IWaterMeterService waterMeterService;

    @Autowired
    private IWaterUserService waterUserService;

    @Autowired
    private IWaterWorkOrderService waterWorkOrderService;

    /**
     * 查询表具安装
     *
     * @param id 表具安装主键
     * @return 表具安装
     */
    @Override
    public WaterMeterinstallinfo selectWaterMeterinstallinfoById(Long id)
    {
        return waterMeterinstallinfoMapper.selectWaterMeterinstallinfoById(id);
    }

    /**
     * 查询表具安装列表
     *
     * @param waterMeterinstallinfo 表具安装
     * @return 表具安装
     */
    @Override
    public List<WaterMeterinstallinfo> selectWaterMeterinstallinfoList(WaterMeterinstallinfo waterMeterinstallinfo)
    {
        return waterMeterinstallinfoMapper.selectWaterMeterinstallinfoList(waterMeterinstallinfo);
    }

    @Override
    public List<WaterMeterinstallinfo> getUserMeterInstListPage(String depid,String uId, String userid,String installState, String username, String useraddress, String meterNo, Integer beginSize, Integer endSize) {
        return waterMeterinstallinfoMapper.selectWaterMeterinstallinfoListPage(depid,uId,userid,installState,username,useraddress,meterNo, beginSize, endSize);
    }

    /**
     * 新增表具安装
     *
     * @param waterMeterinstallinfo 表具安装
     * @return 结果
     */
    @Override
    public int insertWaterMeterinstallinfo(WaterMeterinstallinfo waterMeterinstallinfo)
    {
        //根据用户和表的关联信息 把用户信息录入表档案中
        WaterWorkOrder waterWorkOrder = new WaterWorkOrder();
        WaterMeterinstallinfo waterMeterinstallinfoSelect = new WaterMeterinstallinfo();

        String orderNumber = waterMeterinstallinfo.getOrderNumber();

        waterMeterinstallinfoSelect.setOrderNumber(orderNumber);
        waterWorkOrder.setOrderNumber(orderNumber);
        List<WaterMeterinstallinfo> waterMeterinstallinfoNum = waterMeterinstallinfoMapper.selectWaterMeterinstallinfoList(waterMeterinstallinfoSelect);
        if (waterMeterinstallinfoNum.size()>0){
            //该工单已经创建过，不插入，不更新
            return 0;
        }
        //该工单未创建过，插入，更新
        waterMeterinstallinfo.setCreateTime(DateUtils.getNowDate());
        int installResult = waterMeterinstallinfoMapper.insertWaterMeterinstallinfo(waterMeterinstallinfo);
        if (installResult>0){
            waterWorkOrder.setStatus(2L);
            waterWorkOrderService.updateWaterWorkOrderNumber(waterWorkOrder);
        }
        return installResult;
    }
    // try {
    // }catch (Exception e){
    //     throw new RuntimeException("更多信息更新时发生错误：" + e.getMessage(), e);
    // }
    // //创建表具安装信息时绑定用户和表具的关系(1对1，更改为1对多，去掉)
    // String userId = waterMeterinstallinfo.getUserId();
    // String meterNo = waterMeterinstallinfo.getMeterNo();
    // waterMeter.setMeterNo(meterNo);
    // waterUser.setUserId(userId);
    // String userName = waterUserService.selectWaterUserList(waterUser).get(0).getUserName();
    // WaterMeter waterMeterResult = waterMeterService.selectWaterMeterList(waterMeter).get(0);
    // waterMeterResult.setUserId(userId);
    // waterMeterResult.setUserName(userName);
    // waterMeterService.updateWaterMeterNo(waterMeterResult);
    /**
     * 修改表具安装
     *
     * @param waterMeterinstallinfo 表具安装
     * @return 结果
     */
    @Override
    public int updateWaterMeterinstallinfo(WaterMeterinstallinfo waterMeterinstallinfo)
    {
        return waterMeterinstallinfoMapper.updateWaterMeterinstallinfo(waterMeterinstallinfo);
    }

    @Override
    public int updateWaterMeterinstOrder(WaterMeterinstallinfo waterMeterinstallinfo)
    {
        return waterMeterinstallinfoMapper.updateWaterMeterinstOrder(waterMeterinstallinfo);
    }

    /**
     * 批量删除表具安装
     *
     * @param ids 需要删除的表具安装主键
     * @return 结果
     */
    @Override
    public int deleteWaterMeterinstallinfoByIds(Long[] ids)
    {
        return waterMeterinstallinfoMapper.deleteWaterMeterinstallinfoByIds(ids);
    }

    /**
     * 删除表具安装信息
     *
     * @param id 表具安装主键
     * @return 结果
     */
    @Override
    public int deleteWaterMeterinstallinfoById(Long id)
    {
        return waterMeterinstallinfoMapper.deleteWaterMeterinstallinfoById(id);
    }
}
